home *** CD-ROM | disk | FTP | other *** search
- /* Match rules with nonterminals for bison,
- copyright (C) 1984 Bob Corbett and Richard Stallman
-
- Permission is granted to anyone to make or distribute verbatim copies of this program
- provided that the copyright notice and this permission notice are preserved;
- and provided that the recipient is not asked to waive or limit his right to
- redistribute copies as permitted by this permission notice;
- and provided that anyone possessing an executable copy
- is granted access to copy the source code, in machine-readable form,
- in some reasonable manner.
-
- Permission is granted to distribute derived works or enhanced versions of
- this program under the above conditions with the additional condition
- that the entire derivative or enhanced work
- must be covered by a permission notice identical to this one.
-
- Anything distributed as part of a package containing portions derived
- from this program, which cannot in current practice perform its function usefully
- in the absense of what was derived directly from this program,
- is to be considered as forming, together with the latter,
- a single work derived from this program,
- which must be entirely covered by a permission notice identical to this one
- in order for distribution of the package to be permitted.
-
- In other words, you are welcome to use, share and improve this program.
- You are forbidden to forbid anyone else to use, share and improve
- what you give them. Help stamp out software-hoarding! */
-
- /* set_derives finds, for each variable (nonterminal), which rules can derive it.
- It sets up the value of derives so that
- derives[i - ntokens] points to a vector of rule numbers, terminated with a zero. */
-
- #include <stdio.h>
- #include "new.h"
- #include "types.h"
- #include "gram.h"
-
-
- short **derives;
-
-
- set_derives()
- {
- register int i;
- register int lhs;
- register shorts *p;
- register short *q;
- register shorts **dset;
- register shorts *delts;
-
- dset = NEW2(nvars, shorts *) - ntokens;
- delts = NEW2(nrules + 1, shorts);
-
- p = delts;
- for (i = nrules; i > 0; i--)
- {
- lhs = rlhs[i];
- p->next = dset[lhs];
- p->value = i;
- dset[lhs] = p;
- p++;
- }
-
- derives = NEW2(nvars, short *) - ntokens;
- q = NEW2(nvars + nrules, short);
-
- for (i = ntokens; i < nsyms; i++)
- {
- derives[i] = q;
- p = dset[i];
- while (p)
- {
- *q++ = p->value;
- p = p->next;
- }
- *q++ = -1;
- }
-
- #ifdef DEBUG
- print_derives();
- #endif
-
- FREE(dset + ntokens);
- FREE(delts);
- }
-
-
- free_derives()
- {
- FREE(derives[ntokens]);
- FREE(derives + ntokens);
- }
-
-
-
- #ifdef DEBUG
-
- print_derives()
- {
- register int i;
- register short *sp;
-
- extern char **tags;
-
- printf("\n\n\nDERIVES\n\n");
-
- for (i = ntokens; i < nsyms; i++)
- {
- printf("%s derives", tags[i]);
- for (sp = derives[i]; *sp > 0; sp++)
- {
- printf(" %d", *sp);
- }
- putchar('\n');
- }
-
- putchar('\n');
- }
-
- #endif
-
-